﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using VoicePhone.Common;

namespace VoicePhone.Controls
{
    public class PhoneReceiverButton : BaseButton
    {
        private int TalkDurationSeconds;
        Timer timer = new Timer() { Enabled = true, Interval = 1000 };

        public PhoneReceiverButton()
        {
            this.Data = Geometry.Parse("M0.63900008,28.466 L64.639997,28.466 L64.639997,31.466 L0.63900008,31.466 z M18.388,23.466 L47.388001,23.466 L47.388001,27.466 L18.388,27.466 z M27.801391,9.7903379 C31.602722,9.5227053 36.297057,9.4978078 40.930712,9.999714 44.179979,10.34973 47.337498,10.921462 47.202779,13.210921 L65.904999,13.5 C65.723626,10.863257 61.207648,1.232788 34.998692,0.53837798 7.3821138,-0.19639012 0.96845329,9.8310675 0.87200007,12.485152 L19.119034,12.778735 C19.25032,10.591076 22.051647,10.198686 27.801391,9.7903379 z M2.4405114,19.325616 L2.4485694,19.331383 C2.4485694,19.331383 16.673771,19.500768 16.681176,19.499771 17.806288,19.513409 18.743113,18.91593 18.806241,18.173512 L19.065,14.729722 0.76883669,14.5 C0.76883669,14.5 0.49906955,17.939968 0.50479182,17.940129 L0.50273889,17.948467 C0.44321798,18.7373 1.362223,19.30971 2.4405114,19.325616 z M46.862043,18.437792 L46.868792,18.442619 C46.810465,19.231511 47.722245,19.806354 48.795279,19.820282 L48.804646,19.82698 C48.804646,19.82698 63.442325,20.001587 63.445356,19.999702 64.579101,20.015306 65.513207,19.416436 65.574761,18.675522 L65.831001,15.232745 47.130582,15 C47.130508,14.999827 46.858075,18.434955 46.862043,18.437792 z");
            timer.Elapsed += Timer_Elapsed;
            timer.Stop();
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(new Action(() => { this.TalkDuration = DateTimeHelper.GetStrTime(++this.TalkDurationSeconds); }));
        }

        public string CallName
        {
            get { return (string)GetValue(CallNameProperty); }
            set { SetValue(CallNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CallName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CallNameProperty =
            DependencyProperty.Register("CallName", typeof(string), typeof(PhoneReceiverButton), new PropertyMetadata(""));

        public string CallNo
        {
            get { return (string)GetValue(CallNoProperty); }
            set { SetValue(CallNoProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CallNo.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CallNoProperty =
            DependencyProperty.Register("CallNo", typeof(string), typeof(PhoneReceiverButton), new PropertyMetadata(""));


        public Dock ReceiverPosition
        {
            get { return (Dock)GetValue(ReceiverPositionProperty); }
            set { SetValue(ReceiverPositionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ReceiverPosition.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ReceiverPositionProperty =
            DependencyProperty.Register("ReceiverPosition", typeof(Dock), typeof(PhoneReceiverButton));


        public Geometry Data
        {
            get { return (Geometry)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }

        public string TalkDuration
        {
            get { return (string)GetValue(TalkDurationProperty); }
            set { SetValue(TalkDurationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TalkDuration.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TalkDurationProperty =
            DependencyProperty.Register("TalkDuration", typeof(string), typeof(PhoneReceiverButton), new PropertyMetadata(""));


        // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register("Data", typeof(Geometry), typeof(PhoneReceiverButton));

        public ReceiverStatus ReceiverStatus
        {
            get { return (ReceiverStatus)GetValue(ReceiverStatusProperty); }
            set { SetValue(ReceiverStatusProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ReceiverStatus.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ReceiverStatusProperty =
            DependencyProperty.Register("ReceiverStatus", typeof(ReceiverStatus), typeof(PhoneReceiverButton), new PropertyMetadata(ReceiverStatus.Idle, StatusChanged));


        public string ReceiverStatusName
        {
            get { return (string)GetValue(ReceiverStatusNameProperty); }
            set { SetValue(ReceiverStatusNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ReceiverStatusName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ReceiverStatusNameProperty =
            DependencyProperty.Register("ReceiverStatusName", typeof(string), typeof(PhoneReceiverButton), new PropertyMetadata(""));


        private static void StatusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var phoneReceiverButton = d as PhoneReceiverButton;
            switch (phoneReceiverButton.ReceiverStatus)
            {
                case ReceiverStatus.Idle:
                    phoneReceiverButton.Data = Geometry.Parse("M0.63900008,28.466 L64.639997,28.466 L64.639997,31.466 L0.63900008,31.466 z M18.388,23.466 L47.388001,23.466 L47.388001,27.466 L18.388,27.466 z M27.801391,9.7903379 C31.602722,9.5227053 36.297057,9.4978078 40.930712,9.999714 44.179979,10.34973 47.337498,10.921462 47.202779,13.210921 L65.904999,13.5 C65.723626,10.863257 61.207648,1.232788 34.998692,0.53837798 7.3821138,-0.19639012 0.96845329,9.8310675 0.87200007,12.485152 L19.119034,12.778735 C19.25032,10.591076 22.051647,10.198686 27.801391,9.7903379 z M2.4405114,19.325616 L2.4485694,19.331383 C2.4485694,19.331383 16.673771,19.500768 16.681176,19.499771 17.806288,19.513409 18.743113,18.91593 18.806241,18.173512 L19.065,14.729722 0.76883669,14.5 C0.76883669,14.5 0.49906955,17.939968 0.50479182,17.940129 L0.50273889,17.948467 C0.44321798,18.7373 1.362223,19.30971 2.4405114,19.325616 z M46.862043,18.437792 L46.868792,18.442619 C46.810465,19.231511 47.722245,19.806354 48.795279,19.820282 L48.804646,19.82698 C48.804646,19.82698 63.442325,20.001587 63.445356,19.999702 64.579101,20.015306 65.513207,19.416436 65.574761,18.675522 L65.831001,15.232745 47.130582,15 C47.130508,14.999827 46.858075,18.434955 46.862043,18.437792 z");
                    phoneReceiverButton.ReceiverStatusName = "";
                    phoneReceiverButton.CallNo = "";
                    phoneReceiverButton.CallName = "";
                    phoneReceiverButton.timer.Stop();
                    phoneReceiverButton.TalkDurationSeconds = 0;
                    phoneReceiverButton.TalkDuration = "";
                    break;
                case ReceiverStatus.Talking:
                    phoneReceiverButton.Data = Geometry.Parse("M0.50000002,51.034819 L64.500997,51.034819 L64.500997,54.034819 L0.50000002,54.034819 z M18.249,46.034819 L47.249001,46.034819 L47.249001,50.034819 L18.249,50.034819 z M41.580777,16.644364 C44.763273,18.740422 48.507792,21.571673 51.884158,24.784588 54.25295,27.036048 56.414197,29.407929 54.916704,31.144961 L69.599048,42.733118 C71.056341,40.528213 73.317552,30.134613 52.91768,13.665345 31.424037,-3.6908833 20.238709,0.38015886 18.550166,2.4301071 L32.868154,13.745392 C34.301093,12.087149 36.76491,13.476758 41.580777,16.644364 z M15.641816,8.8170998 L15.644715,8.8265757 C15.644715,8.8265757 26.843003,17.600595 26.849491,17.604301 27.735049,18.298454 28.842176,18.392755 29.343223,17.841284 L31.640326,15.26253 17.244522,3.9681415 C17.244522,3.9681415 14.940995,6.5371734 14.945444,6.5407761 L14.938749,6.5461539 C14.412378,7.1366901 14.794833,8.1495826 15.641816,8.8170998 z M51.471549,35.09049 L51.47398,35.098423 C50.94852,35.689731 51.323759,36.700169 52.167768,37.362924 L52.171141,37.373934 C52.171141,37.373934 63.693949,46.402614 63.697502,46.402957 64.588725,47.103916 65.694536,47.19546 66.19342,46.644228 L68.487905,44.064749 53.772778,32.522441 C53.772825,32.522259 51.47012,35.085826 51.471549,35.09049 z");
                    phoneReceiverButton.ReceiverStatusName = "正在通话";
                    phoneReceiverButton.TalkDuration = DateTimeHelper.GetStrTime(0);
                    phoneReceiverButton.timer.Start();
                    break;
                case ReceiverStatus.Calling:
                    //phoneReceiverButton.Data = Geometry.Parse("M20.881439,7.0962243 C21.424872,7.1030464 21.964901,7.2655745 22.375179,7.5822229 23.195736,8.2155175 23.193295,9.2306747 22.369701,9.8617024 19.609241,11.909372 18.056117,14.714476 18.062144,17.641594 18.0556,20.352663 19.388088,22.96747 21.797503,24.971674 22.323347,25.399706 22.504849,26.008913 22.272686,26.566605 22.040519,27.124296 21.430548,27.544329 20.675756,27.666264 19.920964,27.788204 19.137941,27.59321 18.625771,27.155762 15.499394,24.553854 13.769596,21.160366&#xD;&#xA;13.776018,17.641594 13.770173,13.842828 15.78587,10.202722 19.36727,7.5444608 19.791167,7.2382855 20.338005,7.0894017 20.881439,7.0962243 z M31.781456,2.4484729 C32.277127,2.4399542 32.771907,2.5466025 33.173952,2.7614554 L47.230896,10.269964 57.857174,10.269964 C59.040623,10.269964 59.999998,10.911994 59.999998,11.703979 L60,23.573336 C59.999998,24.365316 59.040623,25.007349 57.857172,25.007349 L46.858044,25.007351 33.178237,32.355251 C32.535328,32.700628 31.653352,32.768543 30.910024,32.529919 30.166696,32.29129 29.693855,31.788441 29.694,31.236721 L29.694,3.8814216 C29.69435,3.3305182 30.166254,2.8284513 30.908128,2.5896912 31.186332,2.5001554 31.484054,2.4535841 31.781456,2.4484729 z M10.68531,0.00037336349 C10.872871,-0.0026772022 11.062481,0.012936831 11.249435,0.048251629 11.997252,0.18951178 12.587357,0.62421978 12.79484,1.1866939 13.002323,1.749168 12.795219,2.3527496 12.252462,2.7673929 7.1341648,6.7460876 4.2745018,12.085033 4.2855759,17.641459 4.2855759,23.157528 7.0562019,28.340717 11.948187,32.2752 12.74477,32.929605 12.697456,33.948046 11.842121,34.558382 10.986784,35.168715 9.6424255,35.143316 8.8282871,34.50144 3.1927547,29.97102 0,23.995413 0,17.641458 0,11.154353 3.3320351,5.0618317 9.1797047,0.5038079 9.5784035,0.18665981 10.122631,0.0095250607 10.68531,0.00037336349 z");
                    phoneReceiverButton.ReceiverStatusName = "正在呼叫";
                    break;
                case ReceiverStatus.Meeting:
                    //phoneReceiverButton.Data = Geometry.Parse("M20.881439,7.0962243 C21.424872,7.1030464 21.964901,7.2655745 22.375179,7.5822229 23.195736,8.2155175 23.193295,9.2306747 22.369701,9.8617024 19.609241,11.909372 18.056117,14.714476 18.062144,17.641594 18.0556,20.352663 19.388088,22.96747 21.797503,24.971674 22.323347,25.399706 22.504849,26.008913 22.272686,26.566605 22.040519,27.124296 21.430548,27.544329 20.675756,27.666264 19.920964,27.788204 19.137941,27.59321 18.625771,27.155762 15.499394,24.553854 13.769596,21.160366&#xD;&#xA;13.776018,17.641594 13.770173,13.842828 15.78587,10.202722 19.36727,7.5444608 19.791167,7.2382855 20.338005,7.0894017 20.881439,7.0962243 z M31.781456,2.4484729 C32.277127,2.4399542 32.771907,2.5466025 33.173952,2.7614554 L47.230896,10.269964 57.857174,10.269964 C59.040623,10.269964 59.999998,10.911994 59.999998,11.703979 L60,23.573336 C59.999998,24.365316 59.040623,25.007349 57.857172,25.007349 L46.858044,25.007351 33.178237,32.355251 C32.535328,32.700628 31.653352,32.768543 30.910024,32.529919 30.166696,32.29129 29.693855,31.788441 29.694,31.236721 L29.694,3.8814216 C29.69435,3.3305182 30.166254,2.8284513 30.908128,2.5896912 31.186332,2.5001554 31.484054,2.4535841 31.781456,2.4484729 z M10.68531,0.00037336349 C10.872871,-0.0026772022 11.062481,0.012936831 11.249435,0.048251629 11.997252,0.18951178 12.587357,0.62421978 12.79484,1.1866939 13.002323,1.749168 12.795219,2.3527496 12.252462,2.7673929 7.1341648,6.7460876 4.2745018,12.085033 4.2855759,17.641459 4.2855759,23.157528 7.0562019,28.340717 11.948187,32.2752 12.74477,32.929605 12.697456,33.948046 11.842121,34.558382 10.986784,35.168715 9.6424255,35.143316 8.8282871,34.50144 3.1927547,29.97102 0,23.995413 0,17.641458 0,11.154353 3.3320351,5.0618317 9.1797047,0.5038079 9.5784035,0.18665981 10.122631,0.0095250607 10.68531,0.00037336349 z");
                    phoneReceiverButton.ReceiverStatusName = "准备会议";
                    break;
            }
        }

        public void Reset(FastCallButton callButton)
        {
            if (callButton == null) return;
            if (callButton.CallStatus == CallStatus.Talking)
                this.ReceiverStatus = ReceiverStatus.Talking;
            if (callButton.CallStatus == CallStatus.Idle)
                this.ReceiverStatus = ReceiverStatus.Idle;
            if (callButton.CallStatus == CallStatus.LordCall)
                this.ReceiverStatus = ReceiverStatus.Calling;
            this.CallNo = callButton.CallStatus == CallStatus.Idle ? "" : callButton.CallNo1;
            this.CallName = callButton.CallStatus == CallStatus.Idle ? "" : callButton.CallName;
        }
    }

    public enum ReceiverStatus
    {
        Idle,
        Calling,
        Talking,
        Meeting
    }

    public enum PhoneReceiverPosition
    {
        Left,
        Right
    }
}
